 /*
 * 二开版本 （由于接口貌似PHP所以在此开发了一套Java后台）
 * Author: 郭少
 * gitee: https://gitee.com/guoshao99/tuodan-manghe?_from=gitee_search
 * QQ：2549789059
 * 微信：2549789059
 */
const Poster = require('../../components/poster/poster/poster');

var e = getApp();

import {
    pay,
    getMemberUser,
    getCode,
    getWxQrCode,
    login
  } from '../request/wx';

Page({
    data: {
        shareQrCode : "",
        avatarUrl: "",
        nickName: "",
        index2_back: "",
        dialogShow: false,
        isMemberUser: false,
        halfShow:false,
        buttons: [{
            type: 'default',
            className: '',
            text: '暂不登陆',
            value: 0
          },
          {
            type: 'primary',
            className: '',
            text: '立即登陆',
            value: 1
          }
        ],
    },
    onShow() {
        let that = this;
        let confog = wx.getStorageSync('config')
        this.setData({
            shenhe: confog.shenhe,
            isPay:confog.isPay
        })
        console.log(this.data.shenhe);
        console.log(this.data.isPay);
        this.getMemberUser();

    },
    
    getMemberUser:function(){
        let that = this;
        let openid = wx.getStorageSync('openid')
        getMemberUser({
            openid
        }).then(res => {
            if(res.code == 200){
                that.setData({
                    isMemberUser:true
                });
                wx.setStorageSync("memberUser", JSON.stringify(res));
            }
        })
    },
 
    view_a3_tap: function(e) {
        wx.navigateTo({
            url: "/pages2/bianji/index"
        });
    },
    index2_tap: function(e) {
        wx.navigateTo({
            url: "/pages2/web_view/index"
        });
    },
    view_a4_tap: function (e) {
        var that = this;
        wx.setClipboardData({
         data: "2549789059",
         success: function (res) {
          wx.showModal({
           title: '至尊服务',
           content: '复制客服微信成功',
           success: function (res) {
            if (res.confirm) {
             console.log('确定')
            } else if (res.cancel) {
             console.log('取消')
            }
           }
          })
         }
        });
       },
    view_a5_tap: function(e) {
        console.log("123"), this.onShareAppMessage();
    },
    view_a8_tap:function(){
        wx.clearStorage({
          success: (res) => {
            wx.showToast({
                title: '清除成功',
                icon: 'none'
            })
          },
        })
        console.log(wx.getStorageSync('openid'));
    },
    onLoad: function(e) {
        var t = this;
        wx.getUserProfile({
            desc: "展示用户信息",
            success: function(e) {
                console.log(e), t.setData({
                    userInfo: e.userInfo,
                    hasUserInfo: !0
                });
            }
        }), wx.request({
            url: "",
            success: function(e) {
                t.setData({
                    index2_back: e.data[2].txt
                });
            }
        });
    },

    /**
     * 海报分享
     */
    sharePoster: function (e) {
       
        let posterConfig = {
            width: 750,
            height: 1334,
            backgroundColor: '#EEDBD7',
            debug: false,
            pixelRatio: 1,
            blocks: [
            ],
            texts: [
                {
                    x: 360,
                    y: 1030,
                    baseLine: 'top',
                    text: '长按识别小程序码',
                    fontSize: 38,
                    color: '#080808',
                },
                {
                    x: 360,
                    y: 1100,
                    baseLine: 'top',
                    text: '距离脱单只差一步',
                    fontSize: 30,
                    color: '#929292',
                },
            ],
            images: [
                {
                    width: 750,
                    height: 1334,
                    x: 10,
                    y: 0,
                    url: '/img/aaaaa.jpg', 
                },
                {
                    width: 220,
                    height: 250,
                    x: 92,
                    y: 980,
                    url: this.data.shareQrCode,   
                }
               
            ]
    
        }
        this.setData({
            posterConfig: posterConfig
        }, () => {
            Poster.create(true); // 入参：true为抹掉重新生成 
        });
    },
    /**
     * 画报生成成功
     */
    onPosterSuccess(e) {
        const {
            detail
        } = e;
        wx.previewImage({
            current: detail,
            urls: [detail]
        })
    },

    /**
     * 画报生成失败
     */
    onPosterFail(err) {
        console.error(err);
    },

    myClick:function(e){
        console.log(e);

        let that = this;
        let isMemberUser = that.data.isMemberUser;
        let openid = wx.getStorageSync('openid');
        if(openid == '' || openid == undefined){
            wx.showToast({
                title: '请先登录',
                icon: 'none'
            })
            this.setData({
                halfShow: true
            })
            return;
        }

        if(!isMemberUser){
            this.setData({ dialogShow : true });
            return;
        }
        let url = "";
        let type = e.target.dataset.type;
        if(type == 1){
            url="./profit/profit";
        }else if(type == 2){
            getWxQrCode({openid}).then(res => {
                console.log(res)
                if(res.code == 200){
                    this.setData({
                        shareQrCode: res.data
                    })
                   
                    this.sharePoster();
                }
                
            })
            return;
        }else if(type == 3){
            url="./team/team";
        }else if(type == 4){
            url="./withdrawal/withdrawal";
        }
       
        console.log('跳转成功');
        wx.navigateTo({
            url: url
        });
            
    },
    
    onConfirm:function(){
        let that = this;
        let type = 3;
        let openid = wx.getStorageSync('openid');
        
        if(openid == '' || openid == undefined){
            wx.showToast({
                title: '请先登录',
                icon: 'none'
            })
            this.setData({
                halfShow: true
            })
            return;
        }
        wx.showLoading({
            title:'加载中...'
        })
        pay({
            openid,
            type
          }).then(pay => {
            wx.requestPayment({
              timeStamp: pay.timeStamp,
              nonceStr: pay.nonceStr,
              package: pay.packageValue,
              signType: pay.signType,
              paySign: pay.paySign,
              success: function (res) {
                console.log(res);
                that.getMemberUser();
                wx.showToast({
                    title: '支付成功，请在一分钟后刷新重试',
                    icon: 'none'
                })
                wx.hideLoading() 
              },
              fail(err) {
                wx.showToast({
                  title: '支付失败',
                  icon: 'none'
                })
                wx.hideLoading() 
              }
            });
        })
    },
    onCancel:function(){
        this.setData({ dialogShow : false });
    },
    onClose:function(e){
        //event.detail: 触发关闭事件的来源，
        //枚举为confirm,cancel,overlay
        this.setData({ dialogShow : false });
    },
    onShareAppMessage: function(e) {
        return {
            title: "网络一线牵，珍惜这段缘",
            path: "/pages/index/index",
            //imageUrl: "http://image4.ailemon.cc/1.png",
            imageUrl:'/img/111.jpeg',
            success: function(e) {
                wx.showToast({
                    title: "分享成功",
                    icon: "success",
                    duration: 2e3
                });
            },
            fail: function(e) {}
        };
    },
    //选择是否授权
  buttontap(e) {
    login(e,this);
  },

});